/*
******************************************************************************************************* 
**  Copyright (C) 2019, 苏州检易生物科技有限公司 
**  All rights reserved. 
** 
**  FileName:       	pmt.c
**  Description:	
**  Author:        	 
**  Version				v0.1
**  Date:           	2019-05-24
**	Function List： 
**  History:         
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                              	调试开关
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                             INCLUDE FILES
*********************************************************************************************************
*/
#include "pmt.h"


/*
*********************************************************************************************************
*                                              	宏定义
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                              	结构体定义
*********************************************************************************************************
*/

 
/*
*********************************************************************************************************
*                                              	函数原型声明
*********************************************************************************************************
*/
static void pmt_recv_callback(void* arg,uint8_t *buff,uint16_t size);

/*
*********************************************************************************************************
*                                              	全局变量定义
*********************************************************************************************************
*/
__attribute__((section(".RAM_D2"))) static pmt_t m_pmt;
/*
*********************************************************************************************************
*                                              	模块静态变量定义
*********************************************************************************************************
*/
static char pmt_init_buf[]="Q\nC017\n";
//uint8_t pmt_init_buf[]={0x43, 0x30, 0x30, 0x30, 0x0A};//,0x4A, 0x0A};	

/*
*********************************************************************************************************
*                                              	函数定义
*********************************************************************************************************
*/
static void pmt_recv_callback(void* arg,uint8_t *buff,uint16_t size)
{
	uint8_t* p_recv=buff;

	SCB_InvalidateDCache_by_Addr((uint32_t *)buff,size);
	while((p_recv-buff)<size)
	{
		switch(p_recv[0]){
			case	PMT_CMD_VER:{p_recv=p_recv+7;break;}
			case	PMT_CMD_QURY:{p_recv=p_recv+1;m_pmt.status=PMT_STATUS_IDLE;Trace_Log("PMT init success!\r\n");break;}
			case	PMT_CMD_PARAM_T:{p_recv=p_recv+3;break;}
			case	PMT_CMD_PARAM_C:{p_recv=p_recv+2;m_pmt.status=PMT_STATUS_IDLE;break;}
			case	PMT_CMD_START:{p_recv=p_recv+6;break;}
			case	PMT_CMD_DATA:{
				if((0xFF&(p_recv[1]+p_recv[2]+p_recv[3]+p_recv[4]))==p_recv[5])
				{
					if(m_pmt.pfn_rlt_cb)
						m_pmt.pfn_rlt_cb(&m_pmt,(p_recv[1]<<24) + (p_recv[2]<<16)+(p_recv[3]<<8)+p_recv[4]);
				}
				p_recv=p_recv+6;
//				m_pmt.status=PMT_STATUS_IDLE;
				break;
				}
			case	PMT_CMD_CLEAR:{p_recv=p_recv+1;break;}
			default:{p_recv=p_recv+1;break;}
		}
	}
}

pmt_t* pmt_init(pmt_rlt_callback pfn_callback)
{
	pmt_t* pmt_handle=&m_pmt;
	pmt_handle->status=PMT_STATUS_UNINIT;
	pmt_handle->pfn_rlt_cb=pfn_callback;
	
	pmt_handle->p_uart=bsp_uart_init(PMT_UART_CHN,PMT_UART_BAUDRATE);
	
	bsp_uart_tx_dma_init(pmt_handle->p_uart,NULL);
	bsp_uart_rx_dma_init(pmt_handle->p_uart,pmt_recv_callback,pmt_handle->rx_buff,PMT_UART_RXBUFF_SIZE);

	bsp_uart_dma_transfer(pmt_handle->p_uart,pmt_init_buf,strlen(pmt_init_buf));
	
	return pmt_handle;
}
pmt_status_t pmt_start_test(uint32_t time_ms,pmt_status_t test_status)
{
	if(m_pmt.status == PMT_STATUS_UNINIT)
	{
		Trace_Log("PMT uninit or init failed!\r\n");
		tcp_err_push(ELEC_ERR_PMT_COUNTER,"PMT counter failed;\n");
//		bsp_uart_dma_transfer(m_pmt.p_uart,pmt_init_buf,sizeof(pmt_init_buf));
//		return PMT_STATUS_ERR;
	}
	memset(m_pmt.tx_buff,0,PMT_UART_TXBUFF_SIZE);
	sprintf((char*)(m_pmt.tx_buff),"T%05d\nJ\n",time_ms/10);
	
	bsp_uart_dma_transfer(m_pmt.p_uart,m_pmt.tx_buff,strlen((char*)m_pmt.tx_buff));
	
	if(test_status==PMT_STATUS_DARK)
		m_pmt.status=PMT_STATUS_DARK;
	else if(test_status==PMT_STATUS_BASE)
		m_pmt.status=PMT_STATUS_BASE;
	else if(test_status==PMT_STATUS_FLASH)
		m_pmt.status=PMT_STATUS_FLASH;
	return m_pmt.status;
}
pmt_status_t pmt_start_multi(uint32_t interval,pmt_status_t test_status)
{
	if(m_pmt.status == PMT_STATUS_UNINIT)
	{
		Trace_Log("PMT uninit or init failed!\r\n");
		tcp_err_push(ELEC_ERR_PMT_COUNTER,"PMT counter failed;\n");
//		bsp_uart_dma_transfer(m_pmt.p_uart,pmt_init_buf,sizeof(pmt_init_buf));
//		return PMT_STATUS_ERR;
	}
	memset(m_pmt.tx_buff,0,PMT_UART_TXBUFF_SIZE);
	sprintf((char*)(m_pmt.tx_buff),"T%05d\nS\n",interval/10);
	
	bsp_uart_dma_transfer(m_pmt.p_uart,m_pmt.tx_buff,strlen((char*)m_pmt.tx_buff));
	
	if(test_status==PMT_STATUS_DARK)
		m_pmt.status=PMT_STATUS_DARK;
	else if(test_status==PMT_STATUS_BASE)
		m_pmt.status=PMT_STATUS_BASE;
	else if(test_status==PMT_STATUS_FLASH)
		m_pmt.status=PMT_STATUS_FLASH;
	return m_pmt.status;
}
pmt_status_t pmt_stop(void)
{
	memset(m_pmt.tx_buff,0,PMT_UART_TXBUFF_SIZE);
	sprintf((char*)(m_pmt.tx_buff),"E\n");
	
	bsp_uart_dma_transfer(m_pmt.p_uart,m_pmt.tx_buff,strlen((char*)m_pmt.tx_buff));
	m_pmt.status = PMT_STATUS_IDLE;
	return m_pmt.status;
}